﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;

namespace DALDWF.App_Code
{
    class clsIneternetConnection
    {
        public bool isConnectionAvailable()
        {
            //build a list of sites to ping, you can use your own
            string[] sitesList = { "www.google.com", "www.microsoft.com" , "www.yahoo.net" };
	
            //create an instance of the System.Net.NetworkInformation Namespace
            Ping ping = new Ping();
	
            //Create an instance of the PingReply object from the same Namespace
            PingReply reply;
	
            //int variable to hold # of pings not successful
            int notReturned = 0;
            bool _success=false;
             try
             {
                 // start a loop that is the lentgh of th string array we
                 // created above
                 for (int i = 0; i <= sitesList.Length; i++)
                 {
                     //use the Send Method of the Ping object to send the
                     //Ping request
                     reply = ping.Send(sitesList[i], 10);

                     //now we check the status, looking for,
                     //of course a Success status
                     if (reply.Status != IPStatus.Success)
                     {
                         //now valid ping so increment
                         notReturned += 1;
                     }

                     //check to see if any pings came back
                     if (notReturned == sitesList.Length)
                     {
                         _success = true;
                         throw new Exception(@"There doest seem to be a network/internet connection.\r\n 
                            Please contact your system administrator");
                     }
                 }

            }
            catch (Exception ex)
            {
                _success = false;
                //_returnMessage = ex.Message;
            }
             return _success;
            
        }

    }
}
